package com.dev;
import android.content.pm.ApplicationInfo;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.LinearLayout;
import java.io.File;
public class AppListTabMenu extends LinearLayout implements OnClickListener{
	WindowManager.LayoutParams wlp=new WindowManager.LayoutParams();
	MainActivity ma;
	AppListTab destination;
	FileBrowserTabMenu.Item itRefresh;
	CheckableItem ciShowSystemApp;
	CheckableItem ciShowUserApp;
	CheckableItem ciSortByName;
	CheckableItem ciSortByNameReverse;
	CheckableItem ciSortByArchiveSize;
	LayoutParams itlp;
	int borderWidth=2;
    AppListTabMenu(MainActivity ma){
		super(ma);
		this.ma=ma;
		setOrientation(VERTICAL);
		setBackground(new FileBrowserTabMenu.BorderDrawable());
		wlp.gravity=Gravity.TOP|Gravity.LEFT;
		wlp.flags=wlp.FLAG_LAYOUT_IN_SCREEN;
		itRefresh=new FileBrowserTabMenu.Item(ma,"刷新");
		itRefresh.setOnClickListener(this);
		ciShowSystemApp=new CheckableItem(ma,"显示系统应用");
		ciShowSystemApp.setOnClickListener(this);
		ciSortByName=new CheckableItem(ma,"按名称顺序排序");
		ciSortByName.setOnClickListener(this);
		ciSortByNameReverse=new CheckableItem(ma,"按名称逆序排序");
		ciSortByNameReverse.setOnClickListener(this);
		ciSortByArchiveSize=new CheckableItem(ma,"按安装包大小排序");
		ciSortByArchiveSize.setOnClickListener(this);
		itlp=new LayoutParams(-1,(int)(30*ma.dp));
		addView(ciShowSystemApp,itlp);
		addView(itRefresh,itlp);
	}
    void show(AppListTab destination,View anchor){
		this.destination=destination;
		Rect rect=new Rect();
		anchor.getGlobalVisibleRect(rect);
		wlp.width=ma.wp*2/3;
		wlp.height=-2;
		wlp.x=rect.left;
		wlp.y=rect.bottom;
		ma.wm.addView(this,wlp);
	}
    void dismiss(){
		ma.wm.removeView(this);
	}
	@Override
	public void onClick(View view) {
		if(view==itRefresh){
			AppListPage ap=destination.ap;
			ap.allApps=ma.pm.getInstalledApplications(0);
			int size=size=ap.allApps.size();
			for(int i=0;i<size;i++){
				ApplicationInfo ai=ap.allApps.get(i);
				if((ai.flags&ai.FLAG_SYSTEM)==0){
					ap.userApps.add(i);
				}
			}
			ap.allApps_name=new String[size];
			ap.allApps_archive=new File[size];
			ap.allApps_icon=new Drawable[size];
			ap.aa.notifyDataSetChanged();
		}else if(view==ciShowSystemApp){
			AppListPage alp=destination.ap;
			if(ciShowSystemApp.box.isSelected()){
				ciShowSystemApp.box.setSelected(false);
				alp.whichWillShow=0;
				alp.aa.notifyDataSetInvalidated();
			}else{
				ciShowSystemApp.box.setSelected(true);
				alp.whichWillShow=1;
				alp.aa.notifyDataSetInvalidated();
			}
		}
	}
	Point touchDown=new Point();
	@Override
	public boolean onTouchEvent(MotionEvent event){
		int x=(int)event.getX();
		int y=(int)event.getY();
		switch(event.getAction()){
			case MotionEvent.ACTION_DOWN:
				touchDown.x=x;
				touchDown.y=y;
				break;
			case MotionEvent.ACTION_UP:
				Rect visible=new Rect();
				getGlobalVisibleRect(visible);
				int[] is=new int[2];
				getLocationOnScreen(is);
				if(touchDown.x>=visible.right||touchDown.x<visible.right||touchDown.y>=visible.bottom||touchDown.y<visible.top){
					dismiss();
				}
		}
		super.onTouchEvent(event);
		return true;
	}
}
